package com.example.hostsys.controller;

import com.example.hostsys.entity.HostMan;
import com.example.hostsys.entity.Student;
import com.example.hostsys.entity.User;
import com.example.hostsys.service.UserService;
import com.example.hostsys.util.ResultSet;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.TreeMap;

@Api(value = "User Controller",tags = "用户接口")
@Controller
@RequestMapping("/user")
public class UserController {
    final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @ApiOperation(value = "获取身份", notes = "", produces = "application/json")
    @PostMapping("/getRole")
    @ResponseBody
    public ResultSet getRole(Authentication auth){
        User user=userService.findUser(auth.getName());
        String[] roles=user.getRole().split(",");
        Map<String,Object> res=new TreeMap<>();
        res.put("username",user.getUsername());
        res.put("name",user.getName());
        for (String role:roles
        ) {
            if(role.equals("ROLE_HOST"))res.put("isHost",true);
            if(role.equals("ROLE_Stu"))res.put("isStu",true);
        }
        return ResultSet.build(res);
    }

    //注册
    @ApiOperation(value = "注册房主", notes = "", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hostMan", value = "{phone,address}", paramType = "json", required = true, dataType = "json")
    })
    @PostMapping("/signHost")
    @ResponseBody
    public ResultSet signHost(@RequestBody HostMan hostMan, Authentication auth) {
        User user=userService.findUser(auth.getName());
        String[] roles=user.getRole().split(",");
        for (String role:roles
             ) {
            if(role.equals("ROLE_HOST"))return ResultSet.fail();
        }
        userService.newHost(hostMan,user);
        return ResultSet.success();
    }

    @ApiOperation(value = "注册学生", notes = "", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "student", value = "{phone,address,birthday}", paramType = "json", required = true, dataType = "json")
    })
    @PostMapping("/signStu")
    @ResponseBody
    public ResultSet signStu(@RequestBody Student student, Authentication auth) {
        User user=userService.findUser(auth.getName());
        String[] roles=user.getRole().split(",");
        for (String role:roles
        ) {
            if(role.equals("ROLE_Stu")) {
                return ResultSet.fail();
            }
        }
        userService.newStu(student,user);
        return ResultSet.success();
    }

    @PostMapping("/test")
    @ResponseBody
    public void test(String username, String password){
        System.out.println(username+password+"aa");
    }
    @GetMapping("/home")
    public String home(){
        return "home";
    }

}
